﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class JobCategoryGateway : DBGateway
    {
        public string SaveJobCategory(JobCategory jobCategory)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =
                    string.Format("insert into tblJobCategory values(@JobCategory)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@JobCategory", SqlDbType.NVarChar).Value = jobCategory.Category;
                CommandObj.ExecuteNonQuery();
                return "New job category created successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }

        public bool HasThisjobCategory(JobCategory jobCategory)
        {
            try
            {
                ConnectionObj.Open();
                string select = String.Format("Select * From tblJobCategory Where JobCategory='{0}'",jobCategory.Category);
                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Employeer Not Save, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }

        public string UpdateJobCategory(JobCategory jobCategory)
        {
            try
            {
                ConnectionObj.Open();
                string select = String.Format("Update tblJobCategory Set JobCategory='{0}' Where Id='{1}'", jobCategory.Category,jobCategory.Id);
                CommandObj.CommandText = select;
                CommandObj.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw new Exception("Job category update failed, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return "Job category update successfully";
        }
    }
}